package com.have.to.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.have.to.vm.BaseVM;
import com.hjq.demo.app.AppActivity;

import java.util.List;


public abstract class AppVmActivity<VM extends BaseVM> extends AppActivity implements BaseUI<VM> {

    /**
     * 当前的ViewModel 如果BaseUI没有BaseVM的实体，
     */
    protected VM mCurrentVM;
    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext = this;
        new BaseUIHelper<VM>().init(this);
        super.onCreate(savedInstanceState);
        initVMListener();
    }


    protected void workOnIdle(final Runnable runnable) {
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                if (runnable != null) {
                    runnable.run();
                }
                return false;
            }
        });
    }


    private void initVMListener() {
        if (mCurrentVM == null) {
            return;
        }
    }

    protected void onDestroy() {
        mContext = null;
        super.onDestroy();
    }


    @Override
    public Context getContext() {
        return mContext;
    }

    //region ViewModel code
    @Override
    public <M extends ViewModel> M getVM(Class<M> cls) {
        return new ViewModelProvider(this,
                ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(cls);
    }

    /**
     * 可选的，初始化多个ViewModel的统一入口
     *
     * @return
     */
    @Override
    public List<BaseVM> initVMList() {
        return null;
    }

    @Override
    public LifecycleOwner getLifecycleOwner() {
        return this;
    }

    @Override
    public void setCurrentVM(VM currentVM) {
        this.mCurrentVM = currentVM;
    }

    @Override
    public void finish() {
        super.finish();
    }

    //endregion
    public void replaceFragment(int resId, Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        if (fm == null) {
            return;
        }
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(resId, fragment);
        transaction.commit();
    }

    /**
     * @return activity是否被销毁
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public boolean isActivityInactivation() {
        return isFinishing() || isDestroyed();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
